package fr.gdi.android.news;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import fr.gdi.android.news.utils.FeedUtils;
import fr.gdi.android.news.utils.PreferenceUtils;
import fr.gdi.android.news.utils.Utils;

public class AddonCleaner extends BroadcastReceiver
{

    @Override
    public void onReceive(Context context, Intent intent)
    {
        String action = intent.getAction();
        if ( TextUtils.equals(action, Intent.ACTION_PACKAGE_REMOVED) )
        {
            Uri uri = intent.getData();
            if ( uri != null )
            {
                String h = uri.toString();
                if ( !TextUtils.isEmpty(h) ) 
                {
                    String host = h.substring("package:".length(), h.length());
                    if ( !TextUtils.isEmpty(host) ) 
                    {
                        String tag = this.getClass().getName();
                        Log.d(tag, "Pacakge " + host + " removed");
                        String pref = FeedUtils.getPackagePrefKey(host);
                        
                        String urls = PreferenceUtils.getStringPreference(context, pref, null);
                        
                        if ( TextUtils.isEmpty(urls) ) return;

                        String[] urlArray = urls.split(PreferenceUtils.URL_SEPARATOR);
                        
                        Log.d(tag, "About to clean up " + urlArray + "(" + urlArray.length + ")");
                        
                        List<String> urlList = Arrays.asList(urlArray);
                        
                        Log.d(tag, "removing pref " + pref);
                        
                        PreferenceUtils.removePreference(context, pref);
                        
                        int[] appWidgetIds = Utils.getAllWidgetIds(context);
                        for (Integer i : appWidgetIds)
                        {
                            removeFeeds(context, urlList, i);
                        }
                        
                        removeFeeds(context, urlList, null);
                    }
                }
            }
        }
    }

    private void removeFeeds(Context context, List<String> urlList, Integer i)
    {
        List<FeedDescription> feeds = new ArrayList<FeedDescription>();
        
        feeds = FeedUtils.getFeedDescriptions(context, i);
        
        List<FeedDescription> descs = new ArrayList<FeedDescription>();
        descs.addAll(feeds);
        Log.d(this.getClass().getName(), "old url list for widget " + i + ": " + descs);
        for (FeedDescription f : feeds)
        {
            for (String url : urlList)
            {
                if ( TextUtils.equals(f.url, url) ) 
                {
                    Log.d(this.getClass().getName(), "removing feed " + url + " for widget " + i);
                    descs.remove(f);
                }                                    
            }
        }
        
        Log.d(this.getClass().getName(), "new url list for widget " + i + ": " + descs);
        
        if ( i == null ) 
        {
            String result = FeedUtils.toCsvString(descs, true);
            FeedUtils.setFeeds(context, null, result.toString());
        }
        else 
        {
            String feedNames = "";
            for (FeedDescription feed : descs)
            {
                if (feedNames.length() > 0) feedNames += ",";
                feedNames += feed.name;
            }
            FeedUtils.setFeeds(context, i, feedNames);
        }
    }
    
}
